// @ts-check
/**
 * Inject babel-loader caller information into the Babel options.
 * @param {import("@babel/core").InputOptions} opts
 * @param {string} target
 * @returns {import("@babel/core").InputOptions}
 */
module.exports = function injectCaller(opts, target) {
  return {
    ...opts,
    caller: Object.assign(
      {
        name: "babel-loader",

        // Provide plugins with insight into webpack target.
        // https://github.com/babel/babel-loader/issues/787
        target,

        // Webpack >= 2 supports ESM and dynamic import.
        supportsStaticESM: true,
        supportsDynamicImport: true,

        // Webpack 5 supports TLA behind a flag. We enable it by default
        // for Babel, and then webpack will throw an error if the experimental
        // flag isn't enabled.
        supportsTopLevelAwait: true,
      },
      opts.caller,
    ),
  };
};
